package me.itsoo.artemis.framework.core.generator;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 随机码生成器
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/30
 */
public class RandomKeyGenerator implements KeyGenerator<String> {

    private static final String LETTER_POOL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    private final int count;

    private RandomKeyGenerator(int count) {
        this.count = count;
    }

    /**
     * 创建实例
     *
     * @param count int
     * @return RandomLetterGenerator
     */
    public static RandomKeyGenerator create(int count) {
        return new RandomKeyGenerator(count);
    }

    @Override
    public String next() {
        final char[] result = new char[this.count];
        final ThreadLocalRandom random = ThreadLocalRandom.current();

        for (int i = 0; i < this.count; ++i) {
            final int index = random.nextInt(LETTER_POOL.length());
            result[i] = LETTER_POOL.charAt(index);
        }

        return new String(result);
    }
}
